﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Projektarbete
{
    //enum MovementDirection { Up, Down, Left, Right };

    class Digger
    {
        public float cloneChance;

        /*
         * 0: up
         * 1: down
         * 2: left
         * 3 right
        */

        public bool active;

        public Point position;

        public Digger(Point position, float cloneChance)
        {
            this.position = position;
            this.cloneChance = cloneChance;

            active = true;
        }

        public bool FoundWall(Tile[][] map)
        {
            if (map[position.Y][position.X].texture == 0)
                return true;
            else
                return false;
        }

        public void Dig(ref Tile[][] map)
        {
            map[position.Y][position.X].texture = 1;
        }

        public void CloneIntoFreePosition(ref Tile[][] map, ref List<Digger> newDiggers, Random random)
        {
            if (random.NextDouble() < cloneChance)
            {
                List<MovementDirection> movementList = new List<MovementDirection>();

                if (map[position.Y - 1][position.X].texture == 0)
                {
                    movementList.Add(MovementDirection.Up);
                }
                if (map[position.Y + 1][position.X].texture == 0)
                {
                    movementList.Add(MovementDirection.Down);
                }
                if (map[position.Y][position.X - 1].texture == 0)
                {
                    movementList.Add(MovementDirection.Left);
                }
                if (map[position.Y][position.X + 1].texture == 0)
                {
                    movementList.Add(MovementDirection.Right);
                }

                if (movementList.Count == 0)
                {
                    return;
                }

                switch (movementList.ElementAt(random.Next(0, movementList.Count)))
                {
                    case MovementDirection.Up:
                        newDiggers.Add(new Digger(new Point(position.X, position.Y - 1), cloneChance));
                        Dig(ref map);
                        break;
                    case MovementDirection.Down:
                        newDiggers.Add(new Digger(new Point(position.X, position.Y + 1), cloneChance));
                        Dig(ref map);
                        break;
                    case MovementDirection.Left:
                        newDiggers.Add(new Digger(new Point(position.X - 1, position.Y), cloneChance));
                        Dig(ref map);
                        break;
                    case MovementDirection.Right:
                        newDiggers.Add(new Digger(new Point(position.X + 1, position.Y), cloneChance));
                        Dig(ref map);
                        break;
                }
            }
        }

        public void MoveToFreePosition(Tile[][] map, Random random, ref Point topLeft, ref Point bottomRight)
        {
            List<MovementDirection> movementList = new List<MovementDirection>();

            if (map[position.Y - 1][position.X].texture == 0)
            {
                movementList.Add(MovementDirection.Up);
            }
            if (map[position.Y + 1][position.X].texture == 0)
            {
                movementList.Add(MovementDirection.Down);
            }
            if (map[position.Y][position.X - 1].texture == 0)
            {
                movementList.Add(MovementDirection.Left);
            }
            if (map[position.Y][position.X + 1].texture == 0)
            {
                movementList.Add(MovementDirection.Right);
            }

            if (movementList.Count == 0)
            {
                active = false;
                return;
            }

            switch (movementList.ElementAt(random.Next(0, movementList.Count)))
            {
                case MovementDirection.Up:
                    position.Y--;

                    if (bottomRight.Y < position.Y)
                        bottomRight.Y = position.Y;

                    if (topLeft.Y > position.Y)
                        topLeft.Y = position.Y;

                    break;
                case MovementDirection.Down:
                    position.Y++;

                    if (bottomRight.Y < position.Y)
                        bottomRight.Y = position.Y;

                    if (topLeft.Y > position.Y)
                        topLeft.Y = position.Y;

                    break;
                case MovementDirection.Left:
                    position.X--;

                    if (bottomRight.X < position.X)
                        bottomRight.X = position.X;

                    if (topLeft.X > position.X)
                        topLeft.X = position.X;

                    break;
                case MovementDirection.Right:
                    position.X++;

                    if (bottomRight.X < position.X)
                        bottomRight.X = position.X;

                    if (topLeft.X > position.X)
                        topLeft.X = position.X;

                    break;
            }
        }

        public void MoveRandomlyIgnoreWalls(Tile[][] map, Random random, ref Point topLeft, ref Point bottomRight)
        {
            switch ((MovementDirection)random.Next(0, 4))
            {
                case MovementDirection.Up:
                    position.Y--;

                    if (bottomRight.Y < position.Y)
                        bottomRight.Y = position.Y;

                    if (topLeft.Y > position.Y)
                        topLeft.Y = position.Y;

                    break;
                case MovementDirection.Down:
                    position.Y++;

                    if (bottomRight.Y < position.Y)
                        bottomRight.Y = position.Y;

                    if (topLeft.Y > position.Y)
                        topLeft.Y = position.Y;

                    break;
                case MovementDirection.Left:
                    position.X--;

                    if (bottomRight.X > position.X)
                        bottomRight.X = position.X;

                    if (topLeft.X < position.X)
                        topLeft.X = position.X;

                    break;
                case MovementDirection.Right:
                    position.X++;

                    if (bottomRight.X < position.X)
                        bottomRight.X = position.X;

                    if (topLeft.X > position.X)
                        topLeft.X = position.X;

                    break;
            }
        }
    }
}
